<?php

namespace App\Http\Controllers;

use App\Models\IpTableModel;
use Illuminate\Http\Request;

class IpController extends Controller
{
    public function index()
    {
        return view('ip.index');
    }

    public function add()
    {
        if (request()->password !== config('app.ip_password')) return failMessage('密码错误');
        if (is_null(request()->ip)) return failMessage('请输入ip地址');
        $ipArr = array_filter(explode(',', request()->ip), function ($v) {
            return $v !== '';
        });
        IpTableModel::query()->delete();
        foreach ($ipArr as $value) {
            if (!preg_match(__('com.ip'), $value)) return failMessage('ip地址:' . $value . '错误');
            IpTableModel::query()->insert([
                'ip' => ip2long($value),
            ]);
        }

        return success();
    }
}
